package com.k2data.k2app.common;

import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.exception.K2ResponseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 各种参数检查, 不通过抛异常
 *
 * @author lidong9144@163.com 17-3-29.
 */
@Component
public class K2Assert {

    private static String MSG_FORMAT = "%s(id: %s)";

    private static Validator validator;

    @Autowired
    public K2Assert(Validator validator) {
        K2Assert.validator = validator;
    }

    /**
     * 检查 deviceId 对应的数据是否存在
     *
     * @param deviceId deviceId
     * @throws K2ResponseException deviceId 对应的数据不存在
     */
    public static void existDeviceId(Long deviceId) throws K2ResponseException {
        if (!validator.checkDeviceID(deviceId)) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format(MSG_FORMAT, ResponseConstant.MSG_DEVICE_ID_NOT_FOUND, deviceId));
        }
    }

    /**
     * 检查 deviceId 对应的数据是否存在, 同时判断是否等于删除关联表标志
     *
     * @param deviceId deviceId
     * @throws K2ResponseException deviceId 对应的数据不存在并且不等于删除关联表标志
     */
    public static void existRelationDeviceId(Long deviceId) throws K2ResponseException {
        if (!Constant.DELETE_TABLE_RELATION.equals(deviceId) && !validator.checkDeviceID(deviceId)) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format(MSG_FORMAT, ResponseConstant.MSG_DEVICE_ID_NOT_FOUND, deviceId));
        }
    }

    /**
     * 检查 deviceId 对应的数据是否存在
     *
     * @param deviceIds deviceIds
     * @throws K2ResponseException deviceId 对应的数据不存在
     */
    public static void existDeviceIds(List<Long> deviceIds) throws K2ResponseException {
        if (!validator.checkDeviceIds(deviceIds).isEmpty()) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format(MSG_FORMAT, ResponseConstant.MSG_DEVICE_ID_NOT_FOUND, deviceIds));
        }
    }

    /**
     * 检查 componentId 对应的数据是否存在
     *
     * @param componentId componentId
     * @throws K2ResponseException componentId 对应的数据不存在
     */
    public static void existComponentId(Long componentId) throws K2ResponseException {
        if (!validator.checkComponentId(componentId)) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format(MSG_FORMAT, ResponseConstant.MSG_COMPONENT_ID_NOT_FOUND, componentId));
        }
    }

    /**
     * 检查 componentId 对应的数据是否存在, 同时判断是否等于删除关联表标志
     *
     * @param componentId componentId
     * @throws K2ResponseException componentId 对应的数据不存在并且不等于删除关联表标志
     */
    public static void existRelationComponentId(Long componentId) throws K2ResponseException {
        if (!Constant.DELETE_TABLE_RELATION.equals(componentId) && !validator.checkComponentId(componentId)) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format(MSG_FORMAT, ResponseConstant.MSG_COMPONENT_ID_NOT_FOUND, componentId));
        }
    }

    /**
     * 检查 customerId 对应的数据是否存在
     *
     * @param customerId customerId
     * @throws K2ResponseException customerId 对应的数据不存在
     */
    public static void existCustomerId(Long customerId) throws K2ResponseException {
        if (!validator.checkCustomerID(customerId)) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format(MSG_FORMAT, ResponseConstant.MSG_CUSTOMER_ID_NOT_FOUND, customerId));
        }
    }

    /**
     * 检查 customerId 对应的数据是否存在, 同时判断是否等于删除关联表标志
     *
     * @param customerId customerId
     * @throws K2ResponseException customerId 对应的数据不存在并且不等于删除关联表标志
     */
    public static void existRelationCustomerId(Long customerId) throws K2ResponseException {
        if (!Constant.DELETE_TABLE_RELATION.equals(customerId) && !validator.checkCustomerID(customerId)) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format(MSG_FORMAT, ResponseConstant.MSG_CUSTOMER_ID_NOT_FOUND, customerId));
        }
    }

    public static void existTemplateDeviceId(long deviceId) throws K2ResponseException {
        if (!validator.checkTemplateDeviceId(deviceId)) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format(MSG_FORMAT, ResponseConstant.MSG_TEMPLATE_DEVICE_ID_NOT_FOUND, deviceId));
        }
    }

    public static void existRelationTemplateDeviceId(long deviceId) throws K2ResponseException {
        if (!Constant.DELETE_TABLE_RELATION.equals(deviceId) && !validator.checkTemplateDeviceId(deviceId)) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format(MSG_FORMAT, ResponseConstant.MSG_TEMPLATE_DEVICE_ID_NOT_FOUND, deviceId));
        }
    }

    public static void checkSensorType(String sensorType) throws K2ResponseException {
        if (validator.checkSensorType(sensorType)) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format("%s(value: %s)", "SensorType already exist!", sensorType));
        }
    }

    public static void checkSensorNameEn(String nameEn) throws K2ResponseException {
        if (validator.checkSensorNameEn(nameEn)) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format("%s(value: %s)", "NameEn already exist!", nameEn));
        }
    }

}
